import urllib2
import zipfile
import os
import shutil
import sys
import tempfile
from StringIO import StringIO

# Check to see if the latestrev currently exists, if not we're going to write one
homedir = os.getenv('USERPROFILE')
scriptDir = '%s\.chrupdate\\' % homedir
revTxtPath = '%slatestrev.txt' % scriptDir
if (os.path.exists(scriptDir) != True):
	print 'Current revision not found, creating new revision file'
	os.makedirs(scriptDir)
	f = open(revTxtPath, 'w')
	f.write('0')
	f.close()

print 'Getting current revision...'
curRevFile = open(revTxtPath) #reads the last revision downloaded with ChromiumUpdater
curRev = curRevFile.read()
curRevFile.close()
print 'Current Revision: %i' % int(curRev)

print 'Getting latest revision...'
response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-xp/LATEST')
latestRev = response.read()
print 'Latest Revision: %i' % (int(latestRev))

# Check to see if we even need to update
if (int(curRev) == int(latestRev)):
	print 'Your Chromium is updated to the latest revision: %i' % int(curRev)
	sys.exit(0)

#workingDir = '/Users/slehan/Desktop/ChromiumUpdate/'
tempPath = tempfile.mkdtemp()
workingDir = '%s\\ChromiumUpdate\\' % tempPath
chromiumZipPath = '%s%i-update.zip' % (workingDir, (int(latestRev)))
#chromiumAppPath = 'chrome-mac\\' #the path of the chromium executable within the zip file
chromiumExtracted = '%s\\chrome-win32' % workingDir
chromiumFinalLoc = '%s\\AppData\\Local\\Chromium\\Application' % homedir
#chromiumAppExtracted = '%' % (workingDir) # path of the extracted executable
#pathToUnzippedApp = '%schrome-mac\\Chromium.app' % (chromiumAppExtracted)

# Make a folder in /tmp that is called ChromiumUpdate
print 'Making directory /tmp/ChromiumUpdate/'
os.mkdir(workingDir)

# we have the revision, now we need to download the zip and extract it
print 'Downloading the latest revision from snapshots...'
latestZip = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-xp/%i/chrome-win32.zip' % (int(latestRev)), '%s' % chromiumZipPath)

output = open(chromiumZipPath, 'w') #delete any current file there
output.write(latestZip.read())
output.close()

# we have the .zip now we need to extract the Chromium.app file, it's in chrome-mac/Chromium.app/
# We will use os.system() to execute unzip on the file
print 'Unzipping Chromium zip file'
#command = 'unzip %s -d %s' % (chromiumZipPath, chromiumAppExtracted)
#os.system(command)
zippedFile = zipfile.ZipFile(chromiumZipPath)
zippedFile.extractall(workingDir)

# Move the files from the newly unzipped folder into homedir\AppData\Local\Chromium\Application
# First we need to remove the current .app from Applications
print 'Moving Chromium from it''s unzipped location to %s' % chromiumFinalLoc

#shutil.rmtree('/Applications/Chromium.app')
# Now we need to move it from pathToUnzippedApp to /Applications
shutil.move(chromiumExtracted, chromiumFinalLoc)

#print 'Launching Chromium'
#os.system('open /Applications/Chromium.app')

print 'Enjoy Revision %i! Please report all bugs to http://code.google.com/p/chromium/issues/entry\nCleaning /tmp while you browse' % (int(latestRev))

# Save the current revision to .chrupdate
latestRevFile = open(revTxtPath, 'w')
latestRevFile.write(latestRev)

shutil.rmtree(tempPath)
sys.exit(0)

